package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.R;
import com.usian.mapper.DeptMapper;
import com.usian.mapper.EmpMapper;
import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    //添加
    @Override
    public R add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.insert(dept);
        return R.success("添加成功");
    }

    //查询
    @Override
    public Page<Dept> findPage(Integer page, Integer pageSize, String name) {
        Page<Dept> page1=new Page<>(page,pageSize);
        QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(name!=null,"name",name);
        queryWrapper.lambda().orderByAsc(Dept::getCreateTime);
        Page<Dept> page2=deptMapper.selectPage(page1,queryWrapper);
        return page2;
    }

    //下拉框1
    @Override
    public List<Dept> list1() {
        return deptMapper.selectList(null);
    }

    //下拉框2
    @Override
    public List<Dept> list2(HttpSession session) {
        Integer id=(Integer) session.getAttribute("login");
        Emp emp=empMapper.findById(id);
        if (emp.getGrade()!=0){
            QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Dept::getId,emp.getDid());
            return deptMapper.selectList(queryWrapper);
        }
        return deptMapper.selectList(null);
    }

}
















